﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace personalplaner.business.dienstplan.plancollection
{
	public class DienstPlanCollection : ObservableCollection<IDienstPlanRow>, IList<IDienstPlanRow>, INotifyPropertyChanged, /*IFocusMover,*/ IDataErrorInfo
	//public class DienstPlanCollection : INotifyPropertyChanged, IFocusMover, IDataErrorInfo
    {
		#region IDataErrorInfo Members

		/// <summary>
		/// Gets an error message indicating what is wrong with this object.
		/// </summary>
		/// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
		public string Error { get; private set; }

		/// <summary>
		/// Gets the error message for the property with the given name.
		/// </summary>
		/// <returns>The error message for the property. The default is an empty string ("").</returns>
		public string this[string property]
		{
			get
			{
				string msg = !string.IsNullOrEmpty(property) ? IsPropertyValid(property) : string.Empty;
				if (string.IsNullOrEmpty(msg))
				{
					this.Error = null;
				}
				else
				{
					this.Error = property;  //Keep track of the most recently validated property that is invalid.
				}
				return msg;
			}
		}

		/// <summary>
		/// Determines whether the specified property is valid.
		/// </summary>
		/// <param name="property">The name of the property.</param>
		/// <returns>if not valid the error message, otherwise null.</returns>
		protected virtual string IsPropertyValid(string property)
		{
			return null;
		}

		List<string> validateProperties;
		private void FillValidateProperties()
		{

			if (validateProperties != null)
				return;

			//get all properties of the instance (properties of child and base class)
			var properties = (from p in this.GetType().GetProperties()
							  where p.DeclaringType != typeof(ItemBase) && p.Name != "DataValue" && p.Name != "Id"
							  select p).ToList();

			validateProperties = (from ia in properties
								  orderby ia.Name
								  select ia.Name).ToList();
		}

		/// <summary>
		/// Determines whether this instance is valid.
		/// Properties of this instance (child and base class) will be verified.
		/// </summary>
		/// <returns>
		///   <c>true</c> if this instance is valid; otherwise, <c>false</c>.
		/// </returns>
		public bool IsValid
		{
			get
			{
				this.FillValidateProperties();
				if (validateProperties == null)
				{
					return true;
				}
				else
				{
					return !validateProperties.Any(prop => !string.IsNullOrEmpty(this[prop]));
				}
			}
		}

		#endregion

		#region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

		protected void RaisePropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion

        //#region IFocusMover Members

        //public event EventHandler<MoveFocusEventArgs> MoveFocus;

        //public void RaiseMoveFocus(string focusedProperty)
        //{
        //    if (this.MoveFocus != null)
        //    {
        //        this.MoveFocus(this, new MoveFocusEventArgs(focusedProperty));
        //    }
        //}

        //#endregion

		public DienstPlanCollection()
		{
			//collection = new ObservableCollection<IDienstPlanRow>();
		}

		public DienstPlanCollection(IEnumerable<IDienstPlanRow> rows)
		{
			//collection = new ObservableCollection<IDienstPlanRow>();

			foreach (var row in rows)
				Add(row);
		}

		//#region Test

		//ObservableCollection<IDienstPlanRow> collection;
		//public ObservableCollection<IDienstPlanRow> Collection
		//{
		//    get
		//    {
		//        return collection;
		//    }
		//}

		//public void Add(IDienstPlanRow row)
		//{
		//    Collection.Add(row);
		//}

		//#endregion

		public override string ToString()
		{
			return Name;
		}

		int _planId;
		public int PlanId
		{
			get { return _planId; }
			set
			{
				_planId = value;
				RaisePropertyChanged("PlanId");
			}
		}

		string _name;
		public string Name
		{
			get { return _name; }
			set
			{
				_name = value;
				RaisePropertyChanged("Name");
			}
		}

		DateTime _von;
		public DateTime Von
		{
			get { return _von; }
			set
			{
				_von = value;
				RaisePropertyChanged("Von");
			}
		}

		DateTime _bis;
		public DateTime Bis
		{
			get { return _bis; }
			set
			{
				_bis = value;
				RaisePropertyChanged("Bis");
			}
		}

		string _schema;
		public string Schema
		{
			get
			{
				return _schema;
			}
			set
			{
				_schema = value;
				RaisePropertyChanged("Schema");
			}
		}
    }
}
